package com.ry.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.webkit.ValueCallback;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

/**
 * 带进度条的水波纹
 *
 * Create by renxianzuo on 2021/4/15
 * Describe
 */
public class MyRippleWidthProgressView extends ConstraintLayout {
    private ValueAnimator animator;
    private ValueAnimator mArcAnimator;
    float mRatio = 0;//0～1
    float mArcRatio = 0;//0～1

    private Paint mPaint;//画笔
    private Paint mCenterPaint;//画笔
    private Paint mArcPaint;//画笔
    private int mArcWidth;
    private int mTotalSeconds = 3*60;
    private RectF rectf;

    private ValueCallback<Integer> mProgressCallback;

    public MyRippleWidthProgressView(@NonNull Context context) {
        super(context);
        initPaint(context);
    }

    public MyRippleWidthProgressView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint(context);
    }

    public MyRippleWidthProgressView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint(context);
    }


    LinearGradient linearGradient;
    private void initPaint(Context context) {
        setWillNotDraw(false);

        mPaint = new Paint();
        mPaint.setColor(Color.argb(0xff * 20 / 100,0xff,0x68,0x9a));
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);

        mCenterPaint = new Paint();
        mCenterPaint.setColor(Color.argb(0xff,0xff,0x89,0x60));
        mCenterPaint.setStyle(Paint.Style.FILL);
        mCenterPaint.setAntiAlias(true);

        mArcWidth = dipToPx(context,2f);
        mArcPaint = new Paint();
        mArcPaint.setColor(Color.argb(0xff,0xff,0x68,0x9a));
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setAntiAlias(true);
        mArcPaint.setStrokeWidth(mArcWidth);


    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.save();

        if(rectf == null){
            rectf = new RectF();
            rectf.set(mArcWidth, mArcWidth, getWidth()-mArcWidth , getHeight()-mArcWidth);
        }
        int radius = (getWidth() - getPaddingLeft() - getPaddingRight())  / 2;
        int inRadius = radius * 3 / 4;

        if(linearGradient == null) {
            linearGradient = new LinearGradient(
                    getWidth() / 2 - inRadius, getHeight() / 2 + inRadius,
                    getWidth() / 2 + inRadius, getHeight() / 2,
                    Color.rgb(0xff, 0x62, 0xa6),
                    Color.rgb(0xff, 0x89, 0x60), Shader.TileMode.MIRROR);
            mCenterPaint.setShader(linearGradient);
        }

        canvas.drawCircle(getWidth()/2,getHeight()/2,radius + getPaddingLeft()* mRatio,mPaint);
        canvas.drawCircle(getWidth()/2,getHeight()/2,inRadius ,mCenterPaint);

        canvas.drawArc(rectf, -90, 360 * mArcRatio, false, mArcPaint);

        canvas.restore();

        super.onDraw(canvas);
    }

    public void startAnimator() {
        int duration = 100;
        if(animator == null) {
            animator = ValueAnimator.ofFloat(0f, 1f);
            animator.setDuration(duration);
            animator.setInterpolator(new LinearInterpolator());
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mRatio = (Float) animation.getAnimatedValue();
                    invalidate();
                }
            });
        }
        animator.start();

        if(mArcAnimator == null) {
            mArcAnimator = ValueAnimator.ofFloat(0f, 1f);
            mArcAnimator.setDuration(mTotalSeconds * 1000);
            mArcAnimator.setInterpolator(new LinearInterpolator());
            mArcAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mArcRatio = (Float) animation.getAnimatedValue();
                    invalidate();

                    if(mProgressCallback != null){
                        mProgressCallback.onReceiveValue((int)(animation.getCurrentPlayTime() / 1000));
                    }
                }
            });
        }
        mArcAnimator.setStartDelay(duration);
        mArcAnimator.start();
    }

    public void stopAnimator(){
        if(animator != null){
            animator.end();
            animator = null;
        }

        if(mArcAnimator != null){
            mArcAnimator.end();
            mArcAnimator = null;
        }
    }

    public void reset(){
        stopAnimator();
        mRatio = 0;
        mArcRatio = 0;
        invalidate();
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 设置录音总时间 单位：秒
     *
     * @param seconds
     */
    public void setTotalSeconds(int seconds){
        this.mTotalSeconds = seconds;
    }

    /**
     * 当前时间0～N秒
     * @param callback
     */
    public void setProgressCallback(ValueCallback<Integer> callback){
        mProgressCallback = callback;
    }
}
